﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;


namespace DoveyAttribute.ValidateExtend
{
    public static class AttributeExtend
    {

        public static bool Validate<T>(this T t)
        {

            Type type = t.GetType();
            foreach (var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
                {
                    AbstractValidateAttribute attribute = (AbstractValidateAttribute)prop.GetCustomAttribute(typeof(AbstractValidateAttribute), true);
                    var value = prop.GetValue(t);
                    if(!attribute.Validate(value))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
    }
}
